package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-09
 */
@RestController
@RequestMapping("/eduservice/edu-video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService eduVideoService;

    @Autowired
    EduVideoService videoService;

    @Autowired
    VodClient vodClient;

    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation("根据id删除小节")
    @DeleteMapping("delVideo/{id}")
    public R delVideo(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        String sourceId = video.getVideoSourceId();
        if (sourceId != null) {
            vodClient.removeAlyVideo(sourceId);
        }
        videoService.removeById(id);
        return R.ok();
    }

    @ApiOperation("根据id查询小节")
    @GetMapping("getVideoById/{chapterId}")
    public R getVideoById(@PathVariable String chapterId) {
        EduVideo eduVideo = eduVideoService.getById(chapterId);
        return R.ok().data("chapter",eduVideo);
    }

    @ApiOperation("修改小节")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

